package com.zr.task.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zh.model.task.SubjectBean;
import com.zr.task.R;

import java.util.Arrays;
import java.util.List;

public class SubjectView extends RelativeLayout {
    private Context context;
    private ToOpen toOpen;
    private SubjectBean subjectBean;
    private String name;

    List<String> SubjectName = Arrays.asList("语文", "数学", "英语", "物理", "化学", "生物", "历史", "政治", "地理");
    List<String> SubjectEnglish = Arrays.asList("Chinese", "Mathematics", "English", "Physical", "Chemistry", "Biology", "History", "Politics", "Geography");
    List<Integer> SubjectImage = Arrays.asList(R.drawable.sub_chinese, R.drawable.sub_mathematics, R.drawable.sub_english, R.drawable.sub_physical,
            R.drawable.sub_chemistry, R.drawable.sub_biology, R.drawable.sub_history, R.drawable.sub_politics, R.drawable.sub_geography);

    public SubjectView(Context context, SubjectBean subjectBean, String name,ToOpen toOpen) {
        super(context);
        this.context = context;
        this.subjectBean = subjectBean;
        this.toOpen = toOpen;
        this.name = name;
        init();

    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.view_subject_one, this);
        TextView nameText = findViewById(R.id.tv_name);
        TextView englishText = findViewById(R.id.tv_english);
        ImageView imageView = findViewById(R.id.iv_bg);
        RelativeLayout relativeLayout = findViewById(R.id.rl_layout);
        nameText.setText(subjectBean.getSubjectName());
        englishText.setText(getEnglish());
        imageView.setImageDrawable(getResources().getDrawable(getImageResources()));
        relativeLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toOpen.toOpen(subjectBean,name);
            }
        });

    }

    private String getEnglish() {
        int index = SubjectName.indexOf(subjectBean.getSubjectName());
        if (index == -1)
            return "ERROR";
        return SubjectEnglish.get(index);
    }

    private int getImageResources() {
        int index = SubjectName.indexOf(subjectBean.getSubjectName());
        if (index == -1)
            return R.drawable.sub_chinese;
        return SubjectImage.get(index);
    }

    public interface ToOpen {
        void toOpen(SubjectBean subjectBean,String name);
    }
}
